// 直流电阻仪
import { unitMap } from '@/config';
import { formateDateTimeNew, formatUnit } from './format';
import type { ConfigItem } from '@/types/tester';
import { storage } from '@/modules/storage';

const repeatConfig = (): ConfigItem[] => {
  // 31 个分接位置
  const pos = Array.from({ length: 31 }, (_, i) => i + 1);
  const res: ConfigItem[][] = pos.map(i => {
    return [
      {
        label: `${i}P AN/AB电阻`,
        key: `pos${i}AN`,
        start: 12 + (i - 1) * 12,
        length: 4,
        dataType: 'float',
        precision: 3,
        readUnit: 'resistanceUnit',
        serverKey: `pos${i}AN`,
        serverUnit: unitMap['0x0C'],
        serverHide: true,
      },
      {
        label: `${i}P BN/BC电阻`,
        key: `pos${i}BN`,
        start: 16 + (i - 1) * 12,
        length: 4,
        dataType: 'float',
        precision: 3,
        readUnit: 'resistanceUnit',
        serverKey: `pos${i}BN`,
        serverUnit: unitMap['0x0C'],
        serverHide: true,
      },
      {
        label: `${i}P CN/CA电阻`,
        key: `pos${i}CN`,
        start: 20 + (i - 1) * 12,
        length: 4,
        dataType: 'float',
        precision: 3,
        readUnit: 'resistanceUnit',
        serverKey: `pos${i}CN`,
        serverUnit: unitMap['0x0C'],
        serverHide: true,
      },
    ];
  });
  return res.flat();
};

export const dcConfig: ConfigItem[] = [
  {
    label: '测试时间',
    key: 'datetime',
    start: 0,
    length: 7,
    dataType: 'unit8',
    format: formateDateTimeNew,
    description: '测试时间',
    serverKey: 'monitorTime',
  },
  {
    label: '油温',
    key: 'temperature',
    start: 385,
    length: 4,
    dataType: 'float',
    serverKey: 'convertTemperature',
    unit: unitMap['0x11'],
    editable: true,
    editType: 'input',
    serverFormat: value => Number(value)
  },
  {
    label: '相别',
    key: 'separation',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'separation',
    serverHide: true,
    editable: true,
    editType: 'select',
    editOptions: ['AN/BN/CN', 'AB/BC/CA'],
  },
  {
    label: '环境温度',
    key: 'ambientTemperature',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'ambientTemperature',
    editable: true,
    editType: 'input',
    unit: unitMap['0x11'],
  },
  {
    label: '空气湿度',
    key: 'airHumidity',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'airHumidity',
    editable: true,
    editType: 'input',
    unit: unitMap['0x08'],
  },
  {
    label: '备注',
    key: 'remark',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'remark',
    editable: true,
    editType: 'input',
  },
  {
    label: '预警提示',
    key: 'warnPrompt',
    start: 0,
    length: 0,
    dataType: 'char',
    serverKey: 'warnPrompt',
    hide: true,
    hideInHistory: false,
  },
  {
    label: '电流',
    key: 'current',
    start: 7,
    length: 4,
    dataType: 'float',
    precision: 3,
    readUnit: 'currentUnit',
    serverKey: 'electricCurrent',
    serverUnit: unitMap['0x09'],
  },
  {
    label: '电流单位',
    key: 'currentUnit',
    start: 11,
    length: 1,
    dataType: 'unit8',
    format: formatUnit,
    hide: true,
  },
  ...repeatConfig(),
  {
    label: '电阻单位',
    key: 'resistanceUnit',
    start: 384,
    length: 1,
    dataType: 'unit8',
    format: formatUnit,
    hide: true,
  },
];

// 直流测试仪电阻和分接位置的特殊处理
export const processDCResult = (raw: ConfigItem[]): ConfigItem[] => {
  const store = (storage.get('dc-value') as ConfigItem[]) || [];

  // 合并相同项
  const res = raw.map(r => {
    if (r.value) return r;
    const i = store.find(v => v.key === r.key);
    if (i && i.value !== '' && i.value !== undefined) {
      return i;
    }
    return r;
  });
  const data = res.filter(r => r.key.startsWith('pos')).filter(r => r.value !== '');
  storage.set('dc-value', data);
  return res;
};
